<template>
  <div class="evaluation-form">
    <div class="title">评估设置</div>
    <el-form class="form" ref="form" :model="info" :rules="rules" label-width="80px">
      <el-form-item label="评估对象" prop="type">
        <el-select v-model="info.type">
          <el-option :value="0" label="攻击"/>
          <el-option :value="1" label="防御"/>
          <el-option :value="2" label="攻防"/>
        </el-select>
      </el-form-item>
    </el-form>
    <selection-form v-show="evalAttack" ref="attackForm" :queue="queue" :model="model" :type="2"/>
    <selection-form v-show="evalDefense" ref="defenseForm" :queue="queue" :model="model" :type="3"/>
  </div>
</template>

<script>
import SelectionForm from './SelectionForm'

export default {
  components: {
    SelectionForm
  },
  props: {
    queue: Array,
    model: String
  },
  data() {
    return {
      info: {
        type: null
      },
      rules: {
        type: [{ required: true, message: '请选择评估对象', trigger: 'change' }]
      }
    }
  },
  computed: {
    evalAttack() {
      return this.info.type == 0 || this.info.type == 2
    },
    evalDefense() {
      return this.info.type == 1 || this.info.type == 2
    }
  },
  methods: {
    validate() {
      return this.$refs.form.validate().then(() => {
        switch (this.info.type) {
          case 0:
            return this.$refs.attackForm.validate()
          case 1:
            return this.$refs.defenseForm.validate()
          case 2:
            return Promise.all([this.$refs.attackForm.validate(), this.$refs.defenseForm.validate()])
        }
      })
    },
    submit() {
      const obj = {
        evalType: this.info.type
      }
      if (this.evalAttack) {
        const submit = this.$refs.attackForm.submit()
        obj.evalAttacks = submit.quests
        obj.evalAttackChanges = submit.changes
      }
      if (this.evalDefense) {
        const submit = this.$refs.defenseForm.submit()
        obj.evalDefenses = submit.quests
        obj.evalDefenseChanges = submit.changes
      }
      return obj
    },
    loadConfig(config) {
      this.info.type = config.evalType
      if (config.evalAttacks) {
        this.$refs.attackForm.loadConfig(config.evalAttacks)
      }
      if (config.evalDefenses) {
        this.$refs.defenseForm.loadConfig(config.evalDefenses)
      }
    },
    deleteQuest(id) {
      this.$refs.attackForm.deleteQuest(id)
      this.$refs.defenseForm.deleteQuest(id)
    }
  }
}
</script>

<style scoped>
.form {
  margin-top: 20px;
}
</style>
